//
//  CLpresntView.m
//  I-want-go
//
//  Created by dllo on 15/10/22.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CLpresntView.h"
#import "BaseModel.h"
#import "CLpresentViewCell.h"
#import "CLpresent.h"
#import "CLproductViewCell.h"
#import "CLproduct.h"
#import "CLtool.h"
#import "CLpresentWebView.h"
#import "CLscreen.h"
@interface CLpresntView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)MBProgressHUD *hud;
@property(nonatomic, retain)UICollectionView *secondView;
@property(nonatomic, retain)NSMutableArray *producArr;
@property(nonatomic, assign)BOOL isclick;
@end

@implementation CLpresntView
- (void)dealloc {
    [_hud release];
    [_secondView release];
    [_producArr release];
    [_arr release];
    [_collectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"精选";
    self.view.backgroundColor = [UIColor redColor];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"正在加载";
    // 第一个collection
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(65 * SWIDTH, 25 * SHEIGHT);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375 * SWIDTH, 30 * SHEIGHT) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.pagingEnabled = YES;
    [self.collectionView registerClass:[CLpresentViewCell class] forCellWithReuseIdentifier:@"raue"];
    self.collectionView.tag = 1000;

    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

        // 第二个
    UICollectionViewFlowLayout *secondFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    secondFlowLayout.itemSize = CGSizeMake(180 * SWIDTH, 180 * SHEIGHT);
    self.secondView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 30 * SHEIGHT, 375 * SWIDTH, (667 - 100) * SHEIGHT) collectionViewLayout:secondFlowLayout];
    [self.view addSubview:self.secondView];
    [_secondView release];
    self.secondView.backgroundColor = [UIColor whiteColor];
    self.secondView.showsVerticalScrollIndicator = NO;
    secondFlowLayout.minimumLineSpacing = 5 * SWIDTH;
    secondFlowLayout.minimumInteritemSpacing = 10 *SHEIGHT;
    [self.secondView registerClass:[CLproductViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.secondView.dataSource = self;
    self.secondView.delegate = self;
    
    
    
//    self.collectionView.header = [MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(header)];
//    // 上啦
//    self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(foot)];
//    
    
    [self createDate];

    [self cret];
    
}
// 分类的解析数据 上面的collection
- (void)createDate {
    NSString *strURL = @"http://open3.bantangapp.com/category/list";
    NSURL *url = [NSURL URLWithString:strURL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *bodyStr = @"app_installtime=1445332836.175734&app_versions=4.2.2&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&os_versions=8.4.1&track_device_info=iPhone4%2C1&track_deviceid=77AFBC3F-B101-47BE-BE6C-C07187B20EB6&v=7";
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        self.arr = [NSMutableArray array];
        self.arr = [CLpresent baseModelByArr:dic[@"data"]];
//        NSLog(@"%@", self.arr);
        [self.collectionView reloadData];
    }];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView.tag == 1000) {
        return self.arr.count;
    }
    else {
        return self.producArr.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView.tag == 1000) {
        CLpresentViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"raue" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor whiteColor];
                    CLpresent *present = self.arr[indexPath.row];
            cell.titleLabel.text = present.name;
            return cell;

    } else {
        CLproductViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
            cell.contentView.backgroundColor = [UIColor whiteColor];
        
        CLproduct *pro = self.producArr[indexPath.row];
        cell.myLabel.text = pro.title;
        [cell.myImage sd_setImageWithURL:[NSURL URLWithString:pro.pic] placeholderImage:[UIImage imageNamed:@"good.png"]];
        cell.priceLabel.text = [NSString stringWithFormat:@"价钱:%@", pro.price];
        cell.priceLabel.textColor = [UIColor grayColor];
        return cell;
    }
    
   }

////////////////////////////////// 解析下面的数据
- (void)cret {
    NSString *stre = @"http://open3.bantangapp.com/product/list";
    NSURL *urli = [NSURL URLWithString:stre];
    NSMutableURLRequest *serequest = [NSMutableURLRequest requestWithURL:urli];
    [serequest setHTTPMethod:@"POST"];
    NSString *body = @"app_installtime=1445332836.175734&app_versions=4.2.2&category=2&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&os_versions=8.4.1&page=0&pagesize=20&screensize=640&track_device_info=iPhone4%2C1&track_deviceid=77AFBC3F-B101-47BE-BE6C-C07187B20EB6&v=7";
    NSData *bodyD = [body dataUsingEncoding:NSUTF8StringEncoding];
    [serequest setHTTPBody:bodyD];
    [NSURLConnection sendAsynchronousRequest:serequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//        NSLog(@"%@", dic[@"data"][@"product"]);
//        NSLog(@"%@", dic);
        self.producArr = [NSMutableArray array];
        self.producArr = [CLproduct baseModelByArr:dic[@"data"][@"product"]];
        [self.secondView reloadData];
//        NSLog(@"%@", self.producArr);
        self.hud.hidden = YES;
    }];
}

///////////第一个collectionview的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView.tag == 1000) {
        CLpresent *pras = self.arr[indexPath.row];
        
        NSString *stre = @"http://open3.bantangapp.com/product/list";
        NSURL *urli = [NSURL URLWithString:stre];
        NSMutableURLRequest *serequest = [NSMutableURLRequest requestWithURL:urli];
        [serequest setHTTPMethod:@"POST"];
        NSString *body = [NSString stringWithFormat:@"app_installtime=1445332836.175734&app_versions=4.2.2&category=%@&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&os_versions=8.4.1&page=0&pagesize=20&screensize=640&track_device_info=iPhone4%2%C1&track_deviceid=77AFBC3F-B101-47BE-BE6C-C07187B20EB6&v=7",pras.mid];
        NSData *bodyD = [body dataUsingEncoding:NSUTF8StringEncoding];
        [serequest setHTTPBody:bodyD];
        [NSURLConnection sendAsynchronousRequest:serequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            //        NSLog(@"%@", dic);
            self.producArr = [NSMutableArray array];
            self.producArr = [CLproduct baseModelByArr:dic[@"data"][@"product"]];
            [self.secondView reloadData];
                    }];

    }
    
    else {
        CLproduct *pro = self.producArr[indexPath.row];
        CLpresentWebView * webVC = [[CLpresentWebView alloc] init];
        [self.navigationController pushViewController:webVC animated:YES];
        webVC.webStr = pro.url;
        
    }
     }



- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
   
    //    self.tabBarController.tabBar.hidden = YES;
    

}

- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
